package com.hnucm.cooklife.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.snackbar.Snackbar;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.BaseFragment;
import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.custom.MySearch;
import com.hnucm.cooklife.ui.activity.ChatActivity;
import com.hnucm.cooklife.ui.activity.DishDetailActivity;
import com.hnucm.cooklife.ui.activity.LoginActivity;
import com.hnucm.cooklife.ui.activity.MomentListActivity;
import com.hnucm.cooklife.ui.activity.OrderDetailActivity;
import com.hnucm.cooklife.ui.activity.PostSomeActivity;
import com.hnucm.cooklife.ui.activity.SearchWordActivity;
import com.hnucm.cooklife.ui.activity.ShopActivity;
import com.hnucm.cooklife.ui.activity.TestActivity;
import com.hnucm.cooklife.ui.activity.UploadDishActivity;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import xyz.sahildave.widget.SearchViewLayout;

import static android.view.View.GONE;
import static com.blankj.utilcode.util.ColorUtils.getColor;

public class HomeFragment extends BaseFragment {
    @BindView(R.id.home_tab)
    QMUITabSegment mSlidingTabLayout;

    @BindView(R.id.home_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.search_box)
    TextView mTextView;

    @BindView(R.id.home_top)
    View mView;

//    @BindView(R.id.home_toolbar)
//    Toolbar mToolbar;
//
//    @BindView(R.id.search_view_container)
//    SearchViewLayout mSearchViewLayout;
//
//    @BindView(R.id.search_expanded_root)
//    LinearLayout mLinearLayout;

    private int mCurTab;

    private Context mContext;

    private List<String> mTabNames = new ArrayList<>();

    private ArrayList<Fragment> mFragmentArrayList = new ArrayList<>();

    public static HomeFragment getInstance(){
        HomeFragment homeFragment = new HomeFragment();
        return homeFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.home_fragment;
    }
    //todo EventBus


    @Override
    protected void init() {
        mContext = getActivity().getApplicationContext();
        initStatusBar();
        setChildViewVisibility(View.VISIBLE);
        mCurTab = 0;
        initFragment();
        intiViewPager();

//        initTabColor();
//        initToolbar();
        initSearch();
        initTop();
    }
    private void initTop() {
        ImageButton m1 = (ImageButton) mView.findViewById(R.id.search_expanded_back_button);
        m1.setImageResource(R.drawable.ic_post);
        m1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UploadDishActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        ImageButton m2 = (ImageButton) mView.findViewById(R.id.search_expanded_magnifying_glass);
        m2.setImageResource(R.drawable.ic_edit);
        m2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(mContext, TestActivity.class);
//                startActivity(intent);
            }
        });
    }

    private void initSearch() {
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchWordActivity.class);
                startActivity(intent);
            }
        });
    }


    private void initStatusBar() {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        if (ColorUtils.calculateLuminance(getColor(R.color.white)) >= 0.5) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void setChildViewVisibility(int visibility) {
        mSlidingTabLayout.setVisibility(visibility);

        mViewPager.setVisibility(visibility);
    }

    private void initFragment() {
        mTabNames.add(0,"关注");
        mTabNames.add(1,"推荐");
        mTabNames.add(2,"分类");
        mFragmentArrayList.add(0,new MomentListActivity());
        mFragmentArrayList.add(1,new RecomFragment());
        mFragmentArrayList.add(2,new ClassiFragment());
    }

    private void intiViewPager() {
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentArrayList == null ? 0: mFragmentArrayList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTabNames.get(position);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurTab = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mSlidingTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
